<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>添加专业</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/layuimini/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>

    <script src="../../../static/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../../static/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script src1="http://local.res.layui.com/layui/release/laydate/dist/laydate.js" charset="utf-8"></script>

    <script type="text/javascript" charset="UTF-8">
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = decodeURI(window.location.search.substr(1).match(reg));
            var arr = r.split(",");
            if (r != null) return arr[2];
            return null;
        }
    </script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="first">
            <div class="layui-form-item">
                <label class="layui-form-label required">专业编号</label>
                <div class="layui-input-block">
                    <input type="text" name="majorId" lay-verify="required" lay-reqtext="专业编号不能为空" placeholder="请输入专业编号"
                           value="" class="layui-input" id="majorId">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">系别</label>
                <div class="layui-input-block">
                    <!--                    <input type="text" name="position" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名"  value="" class="layui-input">-->
                    <select name="department" lay-verify="required" lay-verType="tips" lay-filter="department"
                            id="department">
                        <option value="">请选择系别</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">专业</label>
                <div class="layui-input-block">
                    <input type="text" name="major" lay-verify="required" lay-reqtext="专业不能为空" placeholder="请输入专业"
                           value="" class="layui-input" id="major">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'miniTab', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            $ = layui.jquery;

        //ajax初始化下拉框数据
        $.get("/getDepartment", function (data) {
            $.each(data, function (index, element) {
                $("#department").append("<option value='" + element.departmentName + "'>" + element.departmentName + "</option>");
            });
            layui.form.render('select');
        }, "json");

        //回填数据
        $.get("/getMajorById", {majorId: getQueryString("majorId")}, function (data) {
            form.val('first', {
                'majorId': data.majorId
                , 'department': data.department
                , 'major': data.majorName
            });
        }, "json");

        //事件监听
        form.on('select(department)', function (data) {
            console.log('select: ', this, data);
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let majorId = $("#majorId").val();
            let departmentName = $("#department").val();
            let majorName = $("#major").val();
            $.get("/editMajor", {
                majorId: majorId,
                department: departmentName,
                majorName: majorName
            }, function (data) {
                if (data == "success") {
                    layer.msg('专业修改成功', {
                        icon: 1,
                        time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // parent.layer.closeAll('iframe');
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();//刷新页面
                    });
                } else {
                    layer.msg('专业修改失败，请稍后再试！', {
                        icon: 0,
                        time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // parent.layer.closeAll('iframe');
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                }
            }, "text");
        });

    });
</script>
</body>
</html>